
/*
 * @Author: LBH 1336709799@qq.com
 * @Date: 2024-12-09 09:13:20
 * @LastEditors: LBH 1336709799@qq.com
 * @LastEditTime: 2024-12-13 22:45:45
 * @FilePath: \hm-admin\src\stores\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { resetRouter, routes } from '@/router'
import { asyncRoutes } from '@/router/asyncRoutes'
import type { Profile } from '@/types/user'
import { getLocalToken, removeToken, setLocalToken} from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref, type Ref } from 'vue'
import type { RouteRecordRaw } from 'vue-router'

export const useUserStore = defineStore('hm-user', () => {
  // 优先从本地取
  const token = ref(getLocalToken() || '')

  // 个人信息
  const profile = ref<Profile>({} as Profile)
  const setProfile = (p: Profile) => {
      profile.value = p
  }

  
  const setToken = (t:string) =>{
    token.value = t
    // 本地存一份
    setLocalToken(t)
  }

  const clearUserInfo = () =>{
    token.value = ''
    removeToken()
  }

  return { token, setToken, clearUserInfo, profile, setProfile }
})


export const useMenuStore = defineStore("hm-menu", () => {
  // 菜单列表
  const menuList = ref<RouteRecordRaw[]>([...routes])
  // 动态添加路由
  const setMenuList = (filterRoutes: RouteRecordRaw[]) => {
    menuList.value = [...menuList.value, ...filterRoutes]
  };
  const resetMenu = () => {
      // 重置左侧菜单
      menuList.value = [...routes]
      // 重置路由系统
      resetRouter()
    }
  return { menuList, setMenuList, resetMenu };
});
